/* 
Harvard Dataverse is stupid. You should just download the replication package from my website. - Mitch
Link to more complete (and more useful) replication package.
https://drive.google.com/file/d/1-2EhqKNhNSgXCt0ptvJcIaFhSmeskc69/view
*/

******************
/*	USER INPUTS	*/
******************

* Main folder for the replication data:
global loc_rep "C:\Users\mdown\Dropbox\Research\Union indictments\Replication"
* If you choose to download DIME version 1 (~140GB, not necessary) set the location here (see Readme for details):
global loc_dime "E:\Assorted data\Campaign contributions\Database on Ideology, Money in Politics, and Elections (DIME)"

**********************
/*	AUTO UPDATING	*/
**********************

global loc_raws "${loc_rep}\Data\1 - Raw data - Main substance data"
global loc_lm2 "${loc_rep}\Data\1 - Raw data - Main substance data\LM Filing data"
global loc_rawh "${loc_rep}\Data\2 - Raw data - Helper datasets"
global loc_allcw "${loc_rep}\Data\2 - Raw data - Helper datasets"
global loc_temp "${loc_rep}\Data\3 - Temporary folder"
global loc_odata "${loc_rep}\Data\4 - Cleaned up data"
global loc_prog "${loc_rep}\Programs\3 - Helper files"
global loc_adata "${loc_rep}\Data\5 - Analysis data"
global loc_output "${loc_rep}\Output"
global loc_campdo "${loc_rep}\Data\2 - Raw data - Helper datasets"
/*
sysdir 
adopath + "$loc_prog"
qui: do "$loc_prog\cgmwildboot.ado"
*/

* Figure 1:

cd "$loc_adata"
use dsA, clear

	keep if ucshr >= .4 & ucshr <= .6
	*keep if mainsamp == 1 & pcamT1 == 1 & pcamT0 == 0
	keep if mainsamp == 1
	global dotcount 20
	do "$loc_prog\R_rdgp1_032316.do"
	cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid rid)
	*cgmreg anyindict ucwin lhs rhs lhs2 rhs2 [aw=shrofcdpopincz], cluster(uid rid)
	global ylab "Probability of indictment"
	predict fity, xb
	predict sey, stdp
	gen uby = fity + 1.96*sey
	gen lby = fity - 1.96*sey

	sort x1
	by x1: gen x1ord = _n
	sort x2
	by x2: gen x2ord = _n
	
	save temp1, replace
	collapse (mean) ploty=anyindict [aw=shrofcdpopincz], by(x1)
	gen x1ord = 1
	save temp2, replace
	use temp1, clear
	collapse (mean) ln=fity lnu=uby lnl=lby, by(x2)
	gen x2ord = 1
	save temp3, replace
	use temp1, clear
	merge 1:1 x1 x1ord using temp2
	drop _merge
	merge 1:1 x2 x2ord using temp3
	drop _merge

	graph twoway (rarea lnu lnl x2 if x2 < .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
		(rarea lnu lnl x2 if x2 >= .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
		(line ln x2 if x2 < .5 & x2ord == 1, lcolor(edkblue)) (line ln x2 if x2 >= .5 & x2ord == 1, lcolor(edkblue)) ///
		(scatter ploty x1 if x1ord == 1, mcolor(ebblue)), xline(.5, lcolor(bluishgray)) yscale(range(0 .06)) ylabel(0(.01).06) ///
		legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white))
	*
	cd "$loc_output"
	graph export F1.eps, replace
	
* Figure 2:

cd "$loc_adata"
use dsA, clear

replace Llogmembers = 0 if Lposmembers == 0
replace Llogttl_receipts = 0 if Lposmembers == 0
*replace Llogcam = 0 if Lposcam == 0

replace mainsamp = (ucshr >= .35 & ucshr <= .65 & odoubleprob == 0 & cycle != 2012)

lookfor bn
lookfor bc

foreach n of numlist 1/15 {
  * LINEAR, NO CONTROLS
  qui: cgmreg anyindict c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp == 1 & ucshr >= .5-(`n'/100) & ucshr <= .5+(`n'/100), cluster(uid congdid)
  qui: gen bn`n' = _b[ucwin]
  qui: gen bnu`n' = _b[ucwin] + 1.96*_se[ucwin]
  qui: gen bnl`n' = _b[ucwin] - 1.96*_se[ucwin]

  * LINEAR, CONTROLS
  qui: reghdfe anyindict c.ucwin##c.(runvar) Lposmembers Llogmembers lncam i.ccnt ucraceshr cdpercz presvshrR ucD lntsp ucisincum Lawingap pcamT1 ///
    [aw=shrofcdpopincz] if mainsamp == 1 & ucshr >= .5-(`n'/100) & ucshr <= .5+(`n'/100), absorb(cycle) cluster(uid congdid)
  qui: gen bc`n' = _b[ucwin]
  qui: gen bcu`n' = _b[ucwin] + 1.96*_se[ucwin]
  qui: gen bcl`n' = _b[ucwin] - 1.96*_se[ucwin]
  
  * QUADRATIC, NO CONTROLS
  qui: cgmreg anyindict c.ucwin##c.(runvar runvar2) [aw=shrofcdpopincz] if mainsamp == 1 & ucshr >= .5-(`n'/100) & ucshr <= .5+(`n'/100), cluster(uid congdid)
  qui: gen bnq`n' = _b[ucwin]
  qui: gen bnqu`n' = _b[ucwin] + 1.96*_se[ucwin]
  qui: gen bnql`n' = _b[ucwin] - 1.96*_se[ucwin]
  
  * QUADRATIC, CONTROLS
  qui: reghdfe anyindict c.ucwin##c.(runvar runvar2) Lposmembers Llogmembers lncam i.ccnt ucraceshr cdpercz presvshrR ucD lntsp ucisincum Lawingap pcamT1 ///
    [aw=shrofcdpopincz] if mainsamp == 1 & ucshr >= .5-(`n'/100) & ucshr <= .5+(`n'/100), absorb(cycle) cluster(uid congdid)
  qui: gen bcq`n' = _b[ucwin]
  qui: gen bcqu`n' = _b[ucwin] + 1.96*_se[ucwin]
  qui: gen bcql`n' = _b[ucwin] - 1.96*_se[ucwin]
}

keep bn* bc*
keep if _n == 1
gen id = 1

reshape long bn bnu bnl bc bcu bcl bnq bnqu bnql bcq bcqu bcql, i(id) j(bwidth)

label var bwidth "Bandwidth"

cd "$loc_temp"
save coefest, replace



cd "$loc_temp"
use coefest, clear

preserve
  keep bc* bwidth
  replace bwidth = bwidth + .12
  cd "$loc_temp"
  save temp, replace
restore

drop bc*
replace bwidth = bwidth - .12
cd "$loc_temp"
append using temp


graph twoway (rcap bnu bnl bwidth, lcolor(ebblue)) (scatter bn bwidth, mcolor(ebblue)) ///
	(rcap bcu bcl bwidth, lcolor(edkblue)) (scatter bc bwidth, mcolor(edkblue) msymbol(S)), ///
	legend(order(2 "No controls" 4 "Controls") rows(1) region(lwidth(none))) graphregion(color(white)) plotregion(fcolor(white)) ///
	yscale(range(-.07 .03)) ylabel(-.06(.02).02) xtitle(Bandwidth) ytitle("Estimate and 95% CI")
cd "$loc_output"
graph export F2a.eps, replace

graph twoway (rcap bnqu bnql bwidth, lcolor(ebblue)) (scatter bnq bwidth, mcolor(ebblue)) ///
	(rcap bcqu bcql bwidth, lcolor(edkblue)) (scatter bcq bwidth, mcolor(edkblue) msymbol(S)), ///
	legend(order(2 "No controls" 4 "Controls") rows(1) region(lwidth(none))) graphregion(color(white)) plotregion(fcolor(white)) ///
	yscale(range(-.07 .03)) ylabel(-.06(.02).02) xtitle(Bandwidth) ytitle("Estimate and 95% CI")
cd "$loc_output"
graph export F2b.eps, replace


* Figure 3: 

cd "$loc_adata"
use dsA, clear


replace Llogmembers = 0 if Lposmembers == 0
replace Llogttl_receipts = 0 if Lposmembers == 0
*replace Llogcam = 0 if Lposcam == 0

reghdfe anyindict Lposmembers Llogmembers lncam i.ccnt ucraceshr cdpercz presvshrR ucD lntsp ucisincum Lawingap pcamT1 [aw=shrofcdpopincz] if mainsamp == 1, absorb(cycle, savefe) cluster(uid congdid)
predict fitval, xb

preserve
	cd "$loc_temp"
	keep if ucshr >= .4 & ucshr <= .6
	*keep if mainsamp == 1 & pcamT1 == 1 & pcamT0 == 0
	keep if mainsamp == 1
	global dotcount 36
	replace anyindict = fitval
	do "$loc_prog\R_rdgp1_032316.do"
	cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid rid)
	*cgmreg anyindict ucwin lhs rhs lhs2 rhs2 [aw=shrofcdpopincz], cluster(uid rid)
	global ylab "Predicted probability based on predetermined chars."
	predict fity, xb
	predict sey, stdp
	gen uby = fity + 1.96*sey
	gen lby = fity - 1.96*sey

	sort x1
	by x1: gen x1ord = _n
	sort x2
	by x2: gen x2ord = _n
	
	save temp1, replace
	collapse (mean) ploty=anyindict [aw=shrofcdpopincz], by(x1)
	gen x1ord = 1
	save temp2, replace
	use temp1, clear
	collapse (mean) ln=fity lnu=uby lnl=lby, by(x2)
	gen x2ord = 1
	save temp3, replace
	use temp1, clear
	merge 1:1 x1 x1ord using temp2
	drop _merge
	merge 1:1 x2 x2ord using temp3
	drop _merge

	graph twoway (rarea lnu lnl x2 if x2 < .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
		(rarea lnu lnl x2 if x2 >= .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
		(line ln x2 if x2 < .5 & x2ord == 1, lcolor(edkblue)) (line ln x2 if x2 >= .5 & x2ord == 1, lcolor(edkblue)) ///
		(scatter ploty x1 if x1ord == 1, mcolor(ebblue)), xline(.5, lcolor(bluishgray)) yscale(range(0 .043)) ylabel(0(.01).04) ///
		legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white))
	*
	cd "$loc_output"
	graph export F3.eps, replace
restore

* Figure 4 (Tables B8 and B9):

**************************
/*	CORRUIPTION DATA	*/
**************************

cd "$loc_campdo"
use CampanteDo_MainData, clear

keep if year == 1970
rename state_code stabb

egen GCISCmean1970=rowmean(GCISC1920 GCISC1930 GCISC1940 GCISC1950 GCISC1960 GCISC1970)
gen ALDmean1970 = 1-GCISCmean1970

keep stabb corruptrate_avg ALDmean1970

merge m:1 stabb using cw_state
drop _merge stname sname stabb sticpsr

sum corruptrate_avg, d
gen histatecorr = corruptrate_avg > `r(p50)'
sum ALDmean1970, d
gen histatedist = ALDmean1970 > `r(p50)'
replace histatedist = . if ALDmean1970 == .

tab histatecorr histatedist

rename stfips fipstate

cd "$loc_temp"
save cdstate, replace



**************************
/*	FIGURE AND TABLES	*/
**************************

cd "$loc_adata"
use dsA, clear

cd "$loc_temp"
merge m:1 fipstate using cdstate
drop if _merge == 2
drop _merge

gen bushadmin = (cycle < 2008)

global dotcount 18
local topshr = .6
local botshr = .4

local hvn = 1
foreach v of varlist h_bigu h_histk {
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & `v' == 1
		global ylab "Probability of indictment"
		qui: do "$loc_prog\R_rdgp1_032316.do"
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
		qui: do "$loc_prog\R_rdgp2alt_061719.do"
		di "`v' == 1"
		sum lnu ploty
		graph twoway (rarea lnu lnl x2 if x2 < .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(rarea lnu lnl x2 if x2 >= .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(line ln x2 if x2 < .5 & x2ord == 1, lcolor(edkblue)) (line ln x2 if x2 >= .5 & x2ord == 1, lcolor(edkblue)) ///
			(scatter ploty x1 if x1ord == 1, mcolor(ebblue)), xline(.5, lcolor(bluishgray)) ///
			legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white)) yscale(range(0 .07)) ylabel(0(.02).06)
		*
		cd "$loc_output"
		graph export F4_`hvn'.eps, replace
	restore
	
	local hvn = `hvn' + 1
	
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & `v' == 0
		qui: do "$loc_prog\R_rdgp1_032316.do"
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
		qui: do "$loc_prog\R_rdgp2alt_061719.do"
		di "`v' == 0"
		sum lnu ploty
		graph twoway (rarea lnu lnl x2 if x2 < .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(rarea lnu lnl x2 if x2 >= .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(line ln x2 if x2 < .5 & x2ord == 1, lcolor(edkblue)) (line ln x2 if x2 >= .5 & x2ord == 1, lcolor(edkblue)) ///
			(scatter ploty x1 if x1ord == 1, mcolor(ebblue)), xline(.5, lcolor(bluishgray)) ///
			legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white)) yscale(range(0 .07)) ylabel(0(.02).06)
		*
		cd "$loc_output"
		graph export F4_`hvn'.eps, replace
	restore
	
	local hvn = `hvn' + 1
}

	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & shrofcdpopincz >= .1
		replace shrofcdpopincz = 1
		qui: do "$loc_prog\R_rdgp1_032316.do"
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
		qui: do "$loc_prog\R_rdgp2alt_061719.do"
		di "shrofcdpopincz >= .1"
		sum lnu ploty
		graph twoway (rarea lnu lnl x2 if x2 < .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(rarea lnu lnl x2 if x2 >= .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(line ln x2 if x2 < .5 & x2ord == 1, lcolor(edkblue)) (line ln x2 if x2 >= .5 & x2ord == 1, lcolor(edkblue)) ///
			(scatter ploty x1 if x1ord == 1, mcolor(ebblue)), xline(.5, lcolor(bluishgray)) ///
			legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white)) yscale(range(0 .042)) ylabel(0(.01).04)
		*
		cd "$loc_output"
		graph export F4_`hvn'.eps, replace
	restore
	
	local hvn = `hvn' + 1
	
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & shrofcdpopincz < .1
		replace shrofcdpopincz = 1
		qui: do "$loc_prog\R_rdgp1_032316.do"
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
		qui: do "$loc_prog\R_rdgp2alt_061719.do"
		di "shrofcdpopincz < .1 0"
		sum lnu ploty
		graph twoway (rarea lnu lnl x2 if x2 < .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(rarea lnu lnl x2 if x2 >= .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(line ln x2 if x2 < .5 & x2ord == 1, lcolor(edkblue)) (line ln x2 if x2 >= .5 & x2ord == 1, lcolor(edkblue)) ///
			(scatter ploty x1 if x1ord == 1, mcolor(ebblue)), xline(.5, lcolor(bluishgray)) ///
			legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white)) yscale(range(0 .042)) ylabel(0(.01).04)
		*
		cd "$loc_output"
		graph export F4_`hvn'.eps, replace
	restore
	
	local hvn = `hvn' + 1
*

foreach v of varlist bushadmin {
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & `v' == 1
		qui: do "$loc_prog\R_rdgp1_032316.do"
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
		qui: do "$loc_prog\R_rdgp2alt_061719.do"
		di "`v' == 1"
		sum lnu ploty
		graph twoway (rarea lnu lnl x2 if x2 < .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(rarea lnu lnl x2 if x2 >= .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(line ln x2 if x2 < .5 & x2ord == 1, lcolor(edkblue)) (line ln x2 if x2 >= .5 & x2ord == 1, lcolor(edkblue)) ///
			(scatter ploty x1 if x1ord == 1, mcolor(ebblue)), xline(.5, lcolor(bluishgray)) ///
			legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white)) yscale(range(0 .065)) ylabel(0(.02).06)
		*
		cd "$loc_output"
		graph export F4_`hvn'.eps, replace
	restore
	
	local hvn = `hvn' + 1
	
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & `v' == 0
		qui: do "$loc_prog\R_rdgp1_032316.do"
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
		qui: do "$loc_prog\R_rdgp2alt_061719.do"
		di "`v' == 0"
		sum lnu ploty
		graph twoway (rarea lnu lnl x2 if x2 < .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(rarea lnu lnl x2 if x2 >= .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(line ln x2 if x2 < .5 & x2ord == 1, lcolor(edkblue)) (line ln x2 if x2 >= .5 & x2ord == 1, lcolor(edkblue)) ///
			(scatter ploty x1 if x1ord == 1, mcolor(ebblue)), xline(.5, lcolor(bluishgray)) ///
			legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white)) yscale(range(0 .065)) ylabel(0(.02).06)
		*
		cd "$loc_output"
		graph export F4_`hvn'.eps, replace
	restore
	
	local hvn = `hvn' + 1
}

foreach v of varlist anyindictoy anyindictey Fanyindictoy Fanyindictey {
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1
		replace anyindict = `v'
		qui: do "$loc_prog\R_rdgp1_032316.do"
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
		qui: do "$loc_prog\R_rdgp2alt_061719.do"
		di "`v'"
		sum lnu ploty
		graph twoway (rarea lnu lnl x2 if x2 < .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(rarea lnu lnl x2 if x2 >= .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(line ln x2 if x2 < .5 & x2ord == 1, lcolor(edkblue)) (line ln x2 if x2 >= .5 & x2ord == 1, lcolor(edkblue)) ///
			(scatter ploty x1 if x1ord == 1, mcolor(ebblue)), xline(.5, lcolor(bluishgray)) ///
			legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white)) yscale(range(0 .04)) ylabel(0(.01).04)
		*
		cd "$loc_output"
		graph export F4_`hvn'.eps, replace
	restore
	
	local hvn = `hvn' + 1
}

foreach v of varlist histatecorr histatedist {
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & `v' == 1
		qui: do "$loc_prog\R_rdgp1_032316.do"
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
		qui: do "$loc_prog\R_rdgp2alt_061719.do"
		di "`v' == 1"
		sum lnu ploty
		graph twoway (rarea lnu lnl x2 if x2 < .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(rarea lnu lnl x2 if x2 >= .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(line ln x2 if x2 < .5 & x2ord == 1, lcolor(edkblue)) (line ln x2 if x2 >= .5 & x2ord == 1, lcolor(edkblue)) ///
			(scatter ploty x1 if x1ord == 1, mcolor(ebblue)), xline(.5, lcolor(bluishgray)) ///
			legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white)) yscale(range(0 .08)) ylabel(0(.02).06)
		*
		cd "$loc_output"
		graph export F4_`hvn'.eps, replace
	restore
	
	local hvn = `hvn' + 1
	
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & `v' == 0
		qui: do "$loc_prog\R_rdgp1_032316.do"
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
		qui: do "$loc_prog\R_rdgp2alt_061719.do"
		di "`v' == 0"
		sum lnu ploty
		graph twoway (rarea lnu lnl x2 if x2 < .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(rarea lnu lnl x2 if x2 >= .5 & x2ord == 1, lcolor(white) lwidth(none) fcolor(gs14)) ///
			(line ln x2 if x2 < .5 & x2ord == 1, lcolor(edkblue)) (line ln x2 if x2 >= .5 & x2ord == 1, lcolor(edkblue)) ///
			(scatter ploty x1 if x1ord == 1, mcolor(ebblue)), xline(.5, lcolor(bluishgray)) ///
			legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white)) yscale(range(0 .08)) ylabel(0(.02).06)
		*
		cd "$loc_output"
		graph export F4_`hvn'.eps, replace
	restore
	
	local hvn = `hvn' + 1
}

di `hvn'

estout e1 e2 e3 e4, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid N_rid, fmt(3 3 0 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
estout e5 e6 e7 e8, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid N_rid, fmt(3 3 0 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
estout e9 e10 e11 e12, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid N_rid, fmt(3 3 0 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
estout e13 e14 e15 e16, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid N_rid, fmt(3 3 0 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin)

cd "$loc_output"
estout e1 e2 e3 e4 using TB8_1.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N N_uid N_congdid N_rid, fmt(0 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e" "%&          e" ///
	"N_uid" "N of union-CZ's" "N_rid" "N of elections" "N_congdid" "N of Districts" ")   \\" ")   \\ \cmidrule{2-5}") label
estout e5 e6 e7 e8 using TB8_2.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N N_uid N_congdid N_rid, fmt(0 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e" "%&          e" ///
	"N_uid" "N of union-CZ's" "N_rid" "N of elections" "N_congdid" "N of Districts" ")   \\" ")   \\ \cmidrule{2-5}") label
estout e9 e10 e11 e12 using TB8_3.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N N_uid N_congdid N_rid, fmt(0 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e" "%&          e" ///
	"N_uid" "N of union-CZ's" "N_rid" "N of elections" "N_congdid" "N of Districts" ")   \\" ")   \\ \cmidrule{2-5}") label
estout e13 e14 e15 e16 using TB8_4.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N N_uid N_congdid N_rid, fmt(0 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e" "%&          e" ///
	"N_uid" "N of union-CZ's" "N_rid" "N of elections" "N_congdid" "N of Districts" ")   \\" ")   \\ \cmidrule{2-5}") label
*

local hvn = 1
local topshr = .55
local botshr = .45

foreach v of varlist h_bigu h_histk {
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & `v' == 1
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
	restore
	
	local hvn = `hvn' + 1
	
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & `v' == 0
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
	restore
	
	local hvn = `hvn' + 1
}

	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & shrofcdpopincz >= .1
		replace shrofcdpopincz = 1
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
	restore
	
	local hvn = `hvn' + 1
	
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & shrofcdpopincz < .1
		replace shrofcdpopincz = 1
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
	restore
	
	local hvn = `hvn' + 1
*

foreach v of varlist bushadmin {
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & `v' == 1
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
	restore
	
	local hvn = `hvn' + 1
	
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & `v' == 0
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
	restore
	
	local hvn = `hvn' + 1
}

foreach v of varlist anyindictoy anyindictey Fanyindictoy Fanyindictey {
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1
		replace anyindict = `v'
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
	restore
	
	local hvn = `hvn' + 1
}

foreach v of varlist histatecorr histatedist {
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & `v' == 1
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
	restore
	
	local hvn = `hvn' + 1
	
	preserve
		cd "$loc_temp"
		keep if ucshr >= `botshr' & ucshr <= `topshr' & mainsamp == 1 & `v' == 0
		qui: cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
		estimates store e`hvn'
		qui: unique rid if e(sample) == 1
		qui: estadd scalar N_rid = `r(unique)'
	restore
	
	local hvn = `hvn' + 1
}

di `hvn'

estout e1 e2 e3 e4, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid N_rid, fmt(3 3 0 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
estout e5 e6 e7 e8, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid N_rid, fmt(3 3 0 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
estout e9 e10 e11 e12, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid N_rid, fmt(3 3 0 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
estout e13 e14 e15 e16, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid N_rid, fmt(3 3 0 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin)

cd "$loc_output"
estout e1 e2 e3 e4 using TB9_1.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N N_uid N_congdid N_rid, fmt(0 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e" "%&          e" ///
	"N_uid" "N of union-CZ's" "N_rid" "N of elections" "N_congdid" "N of Districts" ")   \\" ")   \\ \cmidrule{2-5}") label
estout e5 e6 e7 e8 using TB9_2.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N N_uid N_congdid N_rid, fmt(0 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e" "%&          e" ///
	"N_uid" "N of union-CZ's" "N_rid" "N of elections" "N_congdid" "N of Districts" ")   \\" ")   \\ \cmidrule{2-5}") label
estout e9 e10 e11 e12 using TB9_3.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N N_uid N_congdid N_rid, fmt(0 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e" "%&          e" ///
	"N_uid" "N of union-CZ's" "N_rid" "N of elections" "N_congdid" "N of Districts" ")   \\" ")   \\ \cmidrule{2-5}") label
estout e13 e14 e15 e16 using TB9_4.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N N_uid N_congdid N_rid, fmt(0 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e" "%&          e" ///
	"N_uid" "N of union-CZ's" "N_rid" "N of elections" "N_congdid" "N of Districts" ")   \\" ")   \\ \cmidrule{2-5}") label
*



* Figure 5 (Table B11):

cd "$loc_adata"
use dsA, clear

keep if ucshr >= .4 & ucshr <= .6

gen doubleprob = (countL > 0 & countR > 0)
drop rid
egen rid = group(fipstate congdist)

keep if countL + countR >= 2
keep if cycle < 2012
*keep if countL + countR == 2

******************************
/*	DECOMPOSITION: MEANS	*/
******************************

sort unm cz cycle ucwin arunvar
by unm cz cycle ucwin: gen ord = _n
*keep if ord == 1

collapse (mean) anyindict ucshr shrofcdpopincz runvar arunvar doubleprob countL countR closeL closeR, by(unm cz cycle uid ucwin)
gen rid = _n
gen runvar2 = runvar^2

sum countL if ucwin == 0 & doubleprob == 0 [aw=shrofcdpopincz]
sum countR if ucwin == 1 & doubleprob == 0 [aw=shrofcdpopincz]

keep anyindict unm cz cycle ucshr ucwin shrofcdpopincz arunvar doubleprob uid rid

reshape wide ucshr shrofcdpopincz arunvar rid, i(unm cz cycle uid) j(ucwin)

gen shrwt = (shrofcdpopincz0 + shrofcdpopincz1)/2
replace shrwt = shrofcdpopincz0 if shrofcdpopincz1 == .
replace shrwt = shrofcdpopincz1 if shrofcdpopincz0 == .
gen lhs = arunvar0
replace lhs = 0 if arunvar0 == . | doubleprob == 1
gen rhs = arunvar1
replace rhs = 0 if arunvar1 == . | doubleprob == 1
gen lhsd = arunvar0
replace lhsd = 0 if doubleprob == 0
gen rhsd = arunvar1
replace rhsd = 0 if doubleprob == 0
gen ucwin = (arunvar0 == .)
gen uclos = (arunvar1 == .)
replace rid1 = rid0 if rid1 == .
replace rid0 = rid1 if rid0 == .

sum anyindict ucwin uclos lhs rhs lhsd rhsd if doubleprob == 1
sum anyindict ucwin uclos lhs rhs lhsd rhsd if doubleprob == 0 & arunvar0 == .	// No loser
sum anyindict ucwin uclos lhs rhs lhsd rhsd if doubleprob == 0 & arunvar1 == .	// No winner


cgmreg anyindict ucwin uclos lhs rhs lhsd rhsd [aw=shrwt], cluster(uid rid1 rid0)
estimates store e1
lincom _b[ucwin] + _b[_cons]
lincom _b[ucwin] - _b[uclos]
* Protection share:
nlcom (-_b[ucwin])/(_b[uclos]-_b[ucwin])
nlcom (-_b[ucwin])/(_b[uclos]-_b[ucwin]) - 1
nlcom (-_b[ucwin])/(_b[uclos]-_b[ucwin]) - .5

cgmreg anyindict ucwin uclos lhs rhs lhsd rhsd [aw=shrwt] if cycle <= 2006, cluster(uid rid1 rid0)
estimates store e2
lincom _b[ucwin] - _b[uclos]
* Protection share:
nlcom (-_b[ucwin])/(_b[uclos]-_b[ucwin])
nlcom (-_b[ucwin])/(_b[uclos]-_b[ucwin]) - 1
nlcom (-_b[ucwin])/(_b[uclos]-_b[ucwin]) - .5

cgmreg anyindict ucwin uclos lhs rhs lhsd rhsd [aw=shrwt] if cycle >= 2008, cluster(uid rid1 rid0)
estimates store e1
lincom _b[ucwin] - _b[uclos]
* Protection share:
nlcom (-_b[ucwin])/(_b[uclos]-_b[ucwin])
nlcom (-_b[ucwin])/(_b[uclos]-_b[ucwin]) - 1
nlcom (-_b[ucwin])/(_b[uclos]-_b[ucwin]) - .5




preserve
	cd "$loc_temp"
	*keep if doubleprob == 0
	gen ucshr = .
	replace ucshr = .5 - lhs + rhs if doubleprob == 0
	gen lltr = (lhsd < rhsd)
	/*
	replace ucshr = .5 + rhsd if doubleprob == 1 & lltr == 0
	replace ucshr = .5 - lhsd if doubleprob == 1 & lltr == 1
	*/
	sum rhsd if doubleprob == 1, d
	replace ucshr = .5 + (rhsd-lhsd) if doubleprob == 1
	gen shrofcdpopincz = shrwt
	
	gen x1 = .
	
	global dotcount 10
	sum ucshr
	local gap = ceil((`r(max)'-`r(min)')*100)/100
	local scl = `gap'/${dotcount}
	di `scl'
	replace x1 = floor(ucshr/`scl')*`scl' + .5*`scl' if doubleprob == 0
	sum x1 if doubleprob == 0
	
	global dotcount 10
	sum ucshr
	local gap = ceil((`r(max)'-`r(min)')*100)/100
	local scl = `gap'/${dotcount}
	di `scl'
	replace x1 = floor(ucshr/`scl')*`scl' + .5*`scl' if doubleprob == 1
	sum x1 if doubleprob == 1

	cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz] if doubleprob == 0, cluster(uid rid1 rid0)
	estimates store e1
	cgmreg anyindict lhsd rhsd [aw=shrofcdpopincz] if doubleprob == 1, cluster(uid rid1 rid0)
	estimates store e2
	
	gen N = 1
	collapse (mean) ploty=anyindict ucwin (rawsum) N [aw=shrofcdpopincz], by(x1 doubleprob)
	sort doubleprob x1
	
	gen lhs = .5 - x1 if x1 < .5 & doubleprob == 0
	replace lhs = 0 if lhs == . & doubleprob == 0
	gen rhs = x1 - .5 if x1 > .5 & doubleprob == 0
	replace rhs = 0 if rhs == . & doubleprob == 0
	list
	gen lhsd = .5 - x1 if x1 < .5 & doubleprob == 1
	replace lhsd = 0 if lhsd == . & doubleprob == 1
	list
	gen rhsd = x1 - .5 if x1 > .5 & doubleprob == 1
	replace rhsd = 0 if rhsd == . & doubleprob == 1
	
	estimates restore e1
	predict fity0, xb
	estimates restore e2
	predict fity1, xb
	list

	graph twoway (line fity0 x1 if x1 < .5 & doubleprob == 0, lcolor(ebblue) lwidth(thick)) ///
		(line fity0 x1 if x1 > .5 & doubleprob == 0, lcolor(ebblue) lwidth(thick)) ///
		(scatter ploty x1 if doubleprob == 0, mcolor(ebblue) msymbol(D) msize(vlarge)) ///
		(scatter ploty x1 if doubleprob == 1 & x1 >= .46 & x1 <= .54, mcolor(gs14) msymbol(O) msize(vlarge) mlcolor(edkblue)) ///
		, xline(.5, lcolor(bluishgray)) ///
		legend(order(3 "Friends only or enemies only (i.e., won all or lost all)" 4 "Friends and enemies (i.e., won some and lost others)") rows(2)) ytitle("Probability of indictment") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white))
	*
	cd "$loc_output"
	graph export F5.eps, replace
restore



* Figure B1

cd "$loc_adata"
use dsA, clear


preserve
	cd "$loc_temp"
	keep if ucshr >= .4 & ucshr <= .6
	keep if mainsamp == 1
	global dotcount 40
	sum ucshr
	local gap = ceil((`r(max)'-`r(min)')*100)/100
	local scl = `gap'/${dotcount}
	di `scl'
	gen xfit = floor(ucshr/(`scl'/2))*(`scl'/2) + .5*(`scl'/2)
	cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid rid)
	global ylab "Probability of indictment"
	predict fity, xb
	predict sey, stdp
	gen uby = fity + 1.96*sey
	gen lby = fity - 1.96*sey
	
	collapse (mean) ln=fity lnu=uby lnl=lby, by(xfit)
	
	save temp_fit, replace
restore

foreach nn of numlist 10 20 36 {

preserve
	cd "$loc_temp"
	keep if ucshr >= .4 & ucshr <= .6
	keep if mainsamp == 1
	global dotcount `nn'
	sum ucshr
	local gap = ceil((`r(max)'-`r(min)')*100)/100
	local scl = `gap'/${dotcount}
	di `scl'
	gen x`nn' = floor(ucshr/`scl')*`scl' + .5*`scl'
	
	collapse (mean) ploty=anyindict [aw=shrofcdpopincz], by(x`nn')
	
	save temp_x`nn', replace
restore

}


use temp_fit, clear
append using temp_x10

graph twoway (rarea lnu lnl xfit if xfit < .5, lcolor(white) lwidth(none) fcolor(gs15)) ///
	(rarea lnu lnl xfit if xfit >= .5, lcolor(white) lwidth(none) fcolor(gs15)) ///
	(line ln xfit if xfit < .5, lcolor(edkblue)) (line ln xfit if xfit >= .5, lcolor(edkblue)) ///
	(scatter ploty x10, mcolor(ebblue)) ///
	, xline(.5, lcolor(bluishgray)) yscale(range(0 .06)) ylabel(0(.01).06) ///
	legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white))
*
cd "$loc_output"
graph export FB1a.eps, replace

cd "$loc_temp"
use temp_fit, clear
append using temp_x36

graph twoway (rarea lnu lnl xfit if xfit < .5, lcolor(white) lwidth(none) fcolor(gs15)) ///
	(rarea lnu lnl xfit if xfit >= .5, lcolor(white) lwidth(none) fcolor(gs15)) ///
	(line ln xfit if xfit < .5, lcolor(edkblue)) (line ln xfit if xfit >= .5, lcolor(edkblue)) ///
	(scatter ploty x36, mcolor(ebblue)) ///
	, xline(.5, lcolor(bluishgray)) yscale(range(0 .06)) ylabel(0(.01).06) ///
	legend(off) ytitle("${ylab}") xtitle(Union candidate vote share) graphregion(color(white)) plotregion(fcolor(white))
*
cd "$loc_output"
graph export FB1b.eps, replace


* Figure B2:

cd "$loc_adata"
use dsA, clear

preserve
	cd "$loc_temp"
	keep if ucshr >= .4 & ucshr <= .6
	keep if mainsamp == 1
	global dotcount 36
	do "$loc_prog\R_rdgp1_032316.do"
	cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid rid)
	cgmreg anyindict ucwin lhs rhs lhs2 rhs2 [aw=shrofcdpopincz], cluster(uid rid)
	do "$loc_prog\R_rdgp2_032316.do"
	graph export FB2b.eps, replace
restore
preserve
	cd "$loc_temp"
	keep if ucshr >= .45 & ucshr <= .55
	keep if mainsamp == 1
	global dotcount 20
	do "$loc_prog\R_rdgp1_032316.do"
	cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid rid)
	*cgmreg anyindict ucwin lhs rhs lhs2 rhs2 [aw=shrofcdpopincz], cluster(uid rid)
	do "$loc_prog\R_rdgp2_032316.do"
	graph export FB2a.eps, replace
restore

* Figure B3: 

cd "$loc_adata"
use dsA, clear


preserve
	cd "$loc_temp"
	keep if ucshr >= .4 & ucshr <= .6 & mainsamp == 1
	keep if odoubleprob == 0
	global dotcount 36
	global ylab "Probability of indictment"
	do "$loc_prog\R_rdgp1_032316.do"
	replace shrofcdpopincz = 1
	cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
	*cgmreg anyindict ucwin lhs rhs lhs2 rhs2 [aw=shrofcdpopincz], cluster(uid rid)
	do "$loc_prog\R_rdgp2_032316.do"
	graph export FB3a.eps, replace
restore
preserve
	cd "$loc_temp"
	keep if ucshr >= .45 & ucshr <= .55 & mainsamp == 1
	keep if odoubleprob == 0
	global dotcount 20
	global ylab "Probability of indictment"
	do "$loc_prog\R_rdgp1_032316.do"
	replace shrofcdpopincz = 1
	cgmreg anyindict ucwin lhs rhs [aw=shrofcdpopincz], cluster(uid congdid)
	*cgmreg anyindict ucwin lhs rhs lhs2 rhs2 [aw=shrofcdpopincz], cluster(uid rid)
	do "$loc_prog\R_rdgp2_032316.do"
	graph export FB3b.eps, replace
restore


* Figure B4: 

cd "$loc_adata"
use dsA, clear

preserve
  keep if ucshr >= .3 & ucshr <= .7 & odoubleprob == 0 & cycle != 2012
  
  keep ucshr fipstate congdist cycle
  duplicates drop
  
  DCdensity_mitch ucshr, breakpoint(.5) generate(Xj Yj r0 fhat se_fhat) userxlab("Union candidate vote share")
  drop Xj Yj r0 fhat se_fhat
  cd "$loc_output"
  graph export FB4.eps, replace
  
restore

* Figure C1 (Table C1):

* This code is a mess. Sorry. This is the last thing left in the replication package and I'm basically out of energy.

**************************
/*	TC1: COLUMNS 1-3	*/
**************************

cd "$loc_adata"
use dsB, clear

preserve
  keep if indict != . & indict > 0
  keep indict unm modecz trueyear
  rename trueyear year
  collapse (sum) indict, by(unm modecz year)
  cd "$loc_temp"
  save aitrueyear, replace
restore

drop if year == 2015

collapse (sum) members ttl_* mxamt s_* p_* inv_* amt_* lucnt pac_funds outside_audit, by(unm year modecz modeczname)	// Collapse by union-year-CZ


cd "$loc_temp"
merge 1:1 unm modecz year using aitrueyear
drop _merge
replace indict = 0 if indict == .

tab year

gen trueanyindict = (indict > 0)

egen id = group(unm modecz)
tsset id year
tsfill, full

gen taiyear = year if trueanyindict == 1
egen minaiyr = min(taiyear), by(id)
gen anyindict = trueanyindict
replace anyindict = 0 if year != minaiyr

*replace anyindict = trueanyindict

gen mnz = (members != 0 & anyindict == 0)
egen cmnz = sum(mnz), by(id)

foreach v of varlist members ttl_* lucnt anyindict {
  replace `v' = 0 if `v' == .
}

***	LOCAL PART	***	

local w = 3
local wp1 = `w' + 1

gen aiF`wp1'p = 0
foreach n of numlist `w'/2 {
  gen aiF`n' = F`n'.anyindict
}
gen aiL0 = anyindict
foreach n of numlist 1/`w' {
  gen aiL`n' = L`n'.anyindict
}
gen aiL`wp1'p = 0
foreach n of numlist `wp1'/15 {
  replace aiF`wp1' = 1 if F`n'.anyindict == 1
  replace aiL`wp1' = 1 if L`n'.anyindict == 1
}

*** LOCAL OVER	***

tabmiss ai*
foreach v of varlist ai* {
  replace `v' = 0 if `v' == .
}

egen indcnt = sum(anyindict), by(id)

cd "$loc_rawh"
merge m:1 year using cpi_7015
drop if _merge != 3
drop _merge

gen rttl_receipts = ttl_receipts/cpi

foreach v of varlist members rttl_receipts lucnt {
  egen den`v' = mean(`v'), by(id)
  gen n`v' = `v'/den`v'
  sum n`v', d
  replace `v' = `r(p99)' if `v' > `r(p99)'
}


reghdfe nrttl_receipts ai* if cmnz != 0 & nrttl_receipts != . & nlucnt != . & nmembers != . & indcnt > 0, absorb(id id#c.year year) cluster(id)
estimates store final2
test aiF2 aiF3 aiF4p
estadd scalar preF = r(F)
estadd scalar preFp = r(p)
test aiL1 aiL2 aiL3 aiL4p
estadd scalar postF = r(F)
estadd scalar postFp = r(p)
reghdfe nlucnt ai* if cmnz != 0 & nrttl_receipts != . & nlucnt != . & nmembers != . & indcnt > 0, absorb(id id#c.year year) cluster(id)
estimates store final3
test aiF2 aiF3 aiF4p
estadd scalar preF = r(F)
estadd scalar preFp = r(p)
test aiL1 aiL2 aiL3 aiL4p
estadd scalar postF = r(F)
estadd scalar postFp = r(p)

reghdfe nmembers ai* if cmnz != 0 & nrttl_receipts != . & nlucnt != . & nmembers != . & indcnt > 0, absorb(id id#c.year year, savefe) cluster(id)
estimates store final1
test aiF2 aiF3 aiF4p
estadd scalar preF = r(F)
estadd scalar preFp = r(p)
test aiL1 aiL2 aiL3 aiL4p
estadd scalar postF = r(F)
estadd scalar postFp = r(p)

reghdfe nmembers ai* if cmnz != 0 & nrttl_receipts != . & nlucnt != . & nmembers != . & indcnt > 0, absorb(id id#c.year year, savefe) cluster(id)
estimates store e4

gen aiF1 = 0

label var aiF4p " $ Ind_{t+4} $ "
label var aiL4p " $ Ind_{t-4} $ "
label var aiL0 " $ Ind_t $ "
foreach n of numlist 1/3 {
  label var aiL`n' " $ Ind_{t-`n'} $ "
  label var aiF`n' " $ Ind_{t-`n'} $ "
}

estout e4 e5 e6, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(r2 N preF preFp postF postFp, fmt(3 0 2 3 2 3)) starlevels(* .10 ** .05 *** .01)
cd "$loc_output"

estimates restore e4
foreach v of varlist __hd* {
  replace `v' = 0
}
predict memhat, xb
predict sehat, stdp

collapse (first) memhat (first) sehat, by(ai*)

gen year = .
replace year = 4 if aiL4p == 1
replace year = 3 if aiL3 == 1
replace year = 2 if aiL2 == 1
replace year = 1 if aiL1 == 1
replace year = 0 if aiL0 == 1
replace year = -4 if aiF4p == 1
replace year = -3 if aiF3 == 1
replace year = -2 if aiF2 == 1
replace year = -1 if year == .

sum memhat if year == -1
replace memhat = memhat - `r(mean)'

gen upb = memhat + 1.96*sehat
gen lob = memhat - 1.96*sehat


graph twoway (rcap upb lob year if year != -1, lcolor(edkblue)) ///
		(scatter memhat year, mcolor(ebblue)), ///
		legend(off) ytitle(Membership) xtitle(Years since indictment) graphregion(color(white)) plotregion(fcolor(white))
cd "$loc_output"
graph export FC1a.eps, replace



**************************
/*	TC1: COLUMNS 4-6	*/
**************************

cd "$loc_adata"
use dsA, clear

rename cam camT12

drop if uid == .

gen ucrshr = ucraces/cdpercz

gen totclose = (gvshrD >= .4 & gvshrD <= .6)
gen contribclose = camT12*(ucshr >= .4 & ucshr <= .6)

replace anyindict = Lanyindict
replace anyindict = 0 if cycle == 2000

collapse (mean) anyindict ucraces ccnt ucshr gvshrD cz (sum) totclose contribclose camT12, by(uid cycle)

rename anyindict manyindict
gen trueanyindict = (manyindict > 0)

drop if cycle == 2014

tsset uid cycle, delta(2)
descr
tsfill, full
descr

preserve
  keep uid cz
  drop if uid == . | cz == .
  duplicates drop
  cd "$loc_temp"
  save fuckyoustata, replace
restore
drop cz
merge m:1 uid using fuckyoustata
drop _merge
sort uid cycle

foreach v of varlist trueanyindict ucraces ccnt totclose contribclose camT12 {
  replace `v' = 0 if `v' == .
}

gen taiyear = cycle if trueanyindict == 1
egen minaiyr = min(taiyear), by(uid)
gen anyindict = trueanyindict
replace anyindict = 0 if cycle != minaiyr


***	LOCAL PART	***	

local w = 2
local wp1 = `w' + 1

gen aiF`wp1' = 0
foreach n of numlist `w'/2 {
  gen aiF`n' = F`n'.anyindict
}
gen aiL0 = anyindict
foreach n of numlist 1/`w' {
  gen aiL`n' = L`n'.anyindict == 1
}
gen aiL`wp1' = 0
foreach n of numlist `wp1'/15 {
  replace aiF`wp1' = 1 if F`n'.anyindict == 1
  replace aiL`wp1' = 1 if L`n'.anyindict == 1
}

*** LOCAL OVER	***


tabmiss ai*
foreach v of varlist ai* {
  replace `v' = 0 if `v' == .
}

rename cycle year
cd "$loc_rawh"
merge m:1 year using cpi_7015
drop if _merge != 3
drop _merge
rename year cycle

egen indcnt = sum(anyindict), by(uid)
gen rcamT12 = camT12/cpi
gen rcc = contribclose/cpi

gen temp5 = rcamT12 - rcc

foreach v of varlist rcamT12 rcc temp5 {
  egen den`v' = mean(`v'), by(uid)
  gen n`v' = `v'/den`v'
  sum n`v', d
  replace n`v' = `r(p99)' if n`v' > `r(p99)' & n`v' != .
}
gen temp4 = contribclose/camT12

gen temp6 = rcc/denrcamT12
gen temp7 = temp5/denrcamT12

sum dentemp5 denrcc, d

reghdfe nrcc ai* if indcnt > 0 & cycle != 2014, absorb(uid uid#c.cycle cycle) cluster(uid)
estimates store final5
test aiF2 aiF3
estadd scalar preF = r(F)
estadd scalar preFp = r(p)
test aiL1 aiL2 aiL3
estadd scalar postF = r(F)
estadd scalar postFp = r(p)

reghdfe ntemp5 ai* if indcnt > 0 & cycle != 2014, absorb(uid uid#c.cycle cycle) cluster(uid)
estimates store final6
test aiF2 aiF3
estadd scalar preF = r(F)
estadd scalar preFp = r(p)
test aiL1 aiL2 aiL3
estadd scalar postF = r(F)
estadd scalar postFp = r(p)

reghdfe nrcamT12 ai* if indcnt > 0 & cycle != 2014, absorb(uid uid#c.cycle cycle, savefe) cluster(uid)
estimates store final4
test aiF2 aiF3
estadd scalar preF = r(F)
estadd scalar preFp = r(p)
test aiL1 aiL2 aiL3
estadd scalar postF = r(F)
estadd scalar postFp = r(p)

reghdfe nrcamT12 ai* if indcnt > 0 & cycle != 2014, absorb(uid uid#c.cycle cycle, savefe) cluster(uid)
estimates store e4

drop if cycle == 2014

gen aiF1 = 0

estout e4 e5 e6, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(r2 N preF preFp postF postFp, fmt(3 0 2 3 2 3)) starlevels(* .10 ** .05 *** .01)
cd "$loc_output"

estimates restore e4
foreach v of varlist __hd* {
  replace `v' = 0
}
predict memhat, xb
predict sehat, stdp

collapse (first) memhat (first) sehat, by(ai*)

gen year = .
replace year = 3 if aiL3 == 1
replace year = 2 if aiL2 == 1
replace year = 1 if aiL1 == 1
replace year = 0 if aiL0 == 1
replace year = -3 if aiF3 == 1
replace year = -2 if aiF2 == 1
replace year = -1 if year == .

sum memhat if year == -1
replace memhat = memhat - `r(mean)'

gen upb = memhat + 1.96*sehat
gen lob = memhat - 1.96*sehat


graph twoway (rcap upb lob year if year != -1, lcolor(edkblue)) ///
		(scatter memhat year, mcolor(ebblue)), xscale(range(-3 3)) xlabel(-3(1)3) ///
		legend(off) ytitle(Contributions) xtitle(Cycles since indictment) graphregion(color(white)) plotregion(fcolor(white))
cd "$loc_output"
graph export FC1b.eps, replace


**************************
/*	TC1: COLUMNS 7-9	*/
**************************

cd "$loc_adata"
use dsA, clear

tsset obsid cycle, delta(2)
egen did = group(fipstate congdist)
drop if unm == "" | did == .

replace ucD = . if ucwin == .
gen ucR = 1 - ucD 			// Stata's so dumb...
tab ucD ucR, missing
tabmiss anyindict

collapse (sum) anyindict (mean) ucD ucR ucwin gvshrR gvshrD fipstate members lucnt ttl_receipts [aw=shrofcdpopincz], by(unm did cycle)
/*
"Cycle == 2006" refers to political outcomes during 2006 and indictments during 2007/2008.
*/

egen newobsid = group(unm did)
tsset newobsid cycle, delta(2)
tsfill, full
replace anyindict = 0 if anyindict == .

preserve
  keep newobsid unm did fipstate
  drop if unm == "" | did == . | fipstate == .
  duplicates drop
  cd "$loc_temp"
  save fuckyoustata, replace
restore
drop unm did
merge m:1 newobsid using fuckyoustata
drop _merge

foreach v of varlist gvshrD gvshrR {
  egen temp = mean(`v'), by(did cycle)
  replace `v' = temp if `v' == .
  drop temp
}

gen vaiyr = cycle if anyindict > 0		// Election year for any election that came immediately before an indictment (e.g., ind in '07, el in '06)
egen yr1 = min(vaiyr), by(unm did)		// Immediately preceding election for first year that a local of *unm* overlapping did had an indictment
gen ind1 = (yr1 == cycle)				// Indicator for the election year that immediately preceded the indictment
gen vaiD = ucD if anyindict > 0 & ind1 == 1		// Variables for party during last election before indictment.
gen vaiR = ucR if anyindict > 0 & ind1 == 1
egen D1 = mean(vaiD), by(unm did)
egen R1 = mean(vaiR), by(unm did)

tab D1 R1, cell missing
tab yr1

gen DV = D1*gvshrD + R1*gvshrR			// Vote share for the party the union contributed to last before its first indictment

tab cycle if D1 != . & R1 != .
tab cycle if ind1 == 1

rename anyindict sumanyindict
*rename ind1 anyindict
sort newobsid cycle
gen anyindict = L.ind1		// Now the time "0" coefficient should correspond to vote shares for the first one after the indictment.
							// In other words, the 2008 vote shares are associated with the the 2007/2008 indictments (rather than 2009/2010)
replace anyindict = 0 if cycle == 2000



***	LOCAL PART	***	

local wF = 2
local wL = 2


local wp1 = `wF' + 1

gen aiF`wp1' = 0
foreach n of numlist `wF'/2 {
  gen aiF`n' = 0
  replace aiF`n' = 1 if F`n'.anyindict == 1
}
foreach n of numlist `wp1'/15 {
  replace aiF`wp1' = 1 if F`n'.anyindict == 1
}

local wp1 = `wL' + 1

gen aiL0 = anyindict
foreach n of numlist 1/`wL' {
  gen aiL`n' = 0
  replace aiL`n' = 1 if L`n'.anyindict == 1
}
gen aiL`wp1' = 0
foreach n of numlist `wp1'/15 {
  replace aiL`wp1' = 1 if L`n'.anyindict == 1
}

*** LOCAL OVER	***

tabmiss ai*
foreach v of varlist ai* {
  replace `v' = 0 if `v' == .
}

egen sy = group(fipstate cycle)

gen nocomp = (DV == 0 | DV == 1)
egen evernocomp = sum(nocomp), by(did)

reghdfe DV ai*, absorb(newobsid newobsid#c.cycle cycle, savefe) cluster(did)
estimates store fin7
test aiF2 aiF3
estadd scalar preF = r(F)
estadd scalar preFp = r(p)
test aiL1 aiL2 aiL3
estadd scalar postF = r(F)
estadd scalar postFp = r(p)

reghdfe DV ai* if DV > 0 & DV < 1, absorb(newobsid newobsid#c.cycle cycle, savefe) cluster(did)
estimates store fin8
test aiF2 aiF3
estadd scalar preF = r(F)
estadd scalar preFp = r(p)
test aiL1 aiL2 aiL3
estadd scalar postF = r(F)
estadd scalar postFp = r(p)

reghdfe DV ai* if DV > 0 & DV < 1, absorb(newobsid newobsid#c.cycle cycle, savefe) cluster(did)
estimates store e2

gen DVwin = (DV >= .5)

reghdfe DVwin ai* if DV > 0 & DV < 1, absorb(newobsid newobsid#c.cycle cycle, savefe) cluster(did)
estimates store fin9
test aiF2 aiF3
estadd scalar preF = r(F)
estadd scalar preFp = r(p)
test aiL1 aiL2 aiL3
estadd scalar postF = r(F)
estadd scalar postFp = r(p)

reghdfe DVwin ai* if DV > 0 & DV < 1, absorb(newobsid newobsid#c.cycle cycle, savefe) cluster(did)
estimates store e2win

gen aiF1 = 0


foreach v of varlist __hd* {
  replace `v' = 0
}

estimates restore e2

preserve

	predict memhat, xb
	predict sehat, stdp

	collapse (first) memhat (first) sehat, by(ai*)

	gen year = .
	replace year = 3 if aiL3 == 1
	replace year = 2 if aiL2 == 1
	replace year = 1 if aiL1 == 1
	replace year = 0 if aiL0 == 1
	replace year = -3 if aiF3 == 1
	replace year = -2 if aiF2 == 1
	replace year = -1 if year == .

sum memhat if year == -1
replace memhat = memhat - `r(mean)'

	gen upb = memhat + 1.96*sehat
	gen lob = memhat - 1.96*sehat


	graph twoway (rcap upb lob year if year != -1, lcolor(edkblue)) ///
			(scatter memhat year, mcolor(ebblue)), xscale(range(-3 3)) xlabel(-3(1)3) ///
			legend(off) ytitle(Union candidate vote share) xtitle(Cycles since indictment) graphregion(color(white)) plotregion(fcolor(white))
	cd "$loc_output"
	graph export FC1c.eps, replace

restore


estimates restore e2win

preserve

	predict memhat, xb
	predict sehat, stdp

	collapse (first) memhat (first) sehat, by(ai*)

	gen year = .
	replace year = 3 if aiL3 == 1
	replace year = 2 if aiL2 == 1
	replace year = 1 if aiL1 == 1
	replace year = 0 if aiL0 == 1
	replace year = -3 if aiF3 == 1
	replace year = -2 if aiF2 == 1
	replace year = -1 if year == .

sum memhat if year == -1
replace memhat = memhat - `r(mean)'

	gen upb = memhat + 1.96*sehat
	gen lob = memhat - 1.96*sehat


	graph twoway (rcap upb lob year if year != -1, lcolor(edkblue)) ///
			(scatter memhat year, mcolor(ebblue)), xscale(range(-3 3)) xlabel(-3(1)3) ///
			legend(off) ytitle(Probability of union candidate win) xtitle(Cycles since indictment) graphregion(color(white)) plotregion(fcolor(white))
	cd "$loc_output"
	graph export FC1d.eps, replace

restore



*

*********

cd "$loc_output"
estout final1 final2 final3 final4 final5 final6 fin7 fin8 fin9 using TC1.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(r2 N preF preFp postF postFp, fmt(3 0 2 3 2 3)) ///
	starlevels(* .10 ** .05 *** .01) style(tex) replace label ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          final1" "%&          e1" ///
	"N_uid" "N of union-CZ's" "N_rid" "N of elections" "preF" "F Stat (pre)" "postF" "F Stat (post)" "preFp" "p-value" "postFp" "p-value")
*

*


* Table 1: 

cd "$loc_adata"
use dsA, clear
/*
gen ucD = (ucwin == gwinD)
gen ucrace = (ucwin != .)
egen ucraces = sum(ucrace), by(unm cz cycle)
egen totraces = sum(ones), by(unm cz cycle)
egen ucwincnt = sum(ucwin), by(unm cz cycle)
gen ucloscnt = ucraces - ucwincnt
gen ucowinshr = (ucwincnt - ucwin)/(ucraces - 1) if ucrace == 1 & ucraces != .
gen ucowinshr2 = (ucwincnt - ucwin)/(totraces - 1) if ucrace == 1 & totraces > 1
gen ucolosshr2 = (ucloscnt - (1-ucwin))/(totraces - 1) if ucrace == 1 & totraces > 1
gen ucoraceshr = (ucraces - ucrace)/(totraces - 1)
gen runvar3 = runvar^3

drop x
*/
******************************
/*	KEY SAMPLE SELECTION	*/
******************************

replace mainsamp = (ucshr >= .4 & ucshr <= .6)
/*
gen closeL = (ucshr >= .4 & ucshr < .5)
gen closeR = (ucshr > .5 & ucshr <= .6)
egen countL = sum(closeL), by(unm cz cycle)
egen countR = sum(closeR), by(unm cz cycle)
*/
*gen doubleprob = (countL > closeL & countR > closeR)
*replace mainsamp = 0 if doubleprob == 1

drop if cycle == 2014
rename cam camT12

cd "$loc_temp"
save forsumstats, replace


******************
/*	CZ CHARS	*/
******************

cd "$loc_temp"
use forsumstats, clear

gen samp = 1
replace samp = 2 if unm != ""
replace samp = 3 if samp == 2 & camT12 > 0 & camT12 != .
replace samp = 4 if samp == 3 & mainsamp == 1

collapse (max) samp (first) tpopcz (mean) gwinD cdpercz [aw=shrofczpopincd], by(cz)

tab samp
sort samp
tabstat tpopcz gwinD cdpercz, s(mean N sd) by(samp)
sum tpopcz gwinD cdpercz if samp == 1 | samp == 2


**********************
/*	DISTRICT CHARS	*/
**********************

cd "$loc_temp"
use forsumstats, clear

gen LDwin = 1 - LRwin

gen incum_run = (incumR == 1 | incumD == 1)
gen incum_win = ((incumR == 1 & gwinD == 0) | (incumD == 1 & gwinD == 1))

collapse (first) camT12 camR camD tsp LDwin gwinD presvshrR rid ucshr incum_run incum_win (mean) mainsamp winvshr, by(cycle fipstate congdist unm)
gen isunion = (unm != "")
sum camT12 if isunion == 1, d
gen nunioncontributors = (camT12 > 0)
collapse (sum) camT12 camR camD nunioncontributors (first) tsp LDwin gwinD presvshrR incum_run incum_win (mean) mainsamp isunion winvshr, by(cycle fipstate congdist)

gen turatio = camT12/tsp
gen ucsingle = (camR == 0 | camD == 0)
gen contdemshare = camD/(camR + camD)
gen ucdem = (camD > 0)

gen samp = 1
replace samp = 2 if samp == 1 & isunion > 0
replace samp = 3 if samp == 2 & camT12 > 0
replace samp = 4 if samp == 3 & mainsamp > 0

tab samp

sum incum_win if incum_run == 1
sum incum_win if incum_run == 1 & winvshr < .6
sum incum_win if incum_run == 1 & winvshr < .52

tabstat gwinD LDwin ucsingle contdemshare ucdem camT12 turatio presvshrR incum_run incum_win nunioncontributors, s(mean N sd) by(samp)

collapse (max) samp, by(fipstate congdist)

tab samp


******************
/*	UNION CHARS	*/
******************

cd "$loc_temp"
use forsumstats, clear

collapse (sum) camT12, by(unm)

gen evercont = (camT12 > 0)
tab evercont
drop camT12

merge 1:m unm using forsumstats
drop _merge

gen samp = .
replace samp = 2 if evercont == 0
replace samp = 3 if evercont == 1 & mainsamp == 0
replace samp = 4 if evercont == 1 & mainsamp == 1

gen nowcont = (camT12 > 0)
gen ndist = 1

collapse (first) members ttl_receipts lucnt anyindict indict (sum) contcnt=nowcont ndist (max) samp mainsamp, by(unm cz cycle)

tab samp

gen ncz = 1
gen dshr = contcnt/ndist		// Share of districts within CZ receiving contributions (might be zero)
replace dshr = . if dshr == 0
gen czwcont = (contcnt > 0)		// Share of CZ's with at least one district receiving contributions

collapse (rawsum) ncz indict members ttl_receipts lucnt (max) anyindict samp (mean) aicz=anyindict mainsamp dshr czwcont, by(unm cycle)

tab samp

collapse (mean) ncz indict members ttl_receipts lucnt aicz mainsamp dshr czwcont (max) anyindict samp, by(unm)

tab samp

tabstat members ttl_receipts lucnt ncz dshr czwcont indict aicz anyindict, s(mean N sd) by(samp)


*


* Table 2:

cd "$loc_adata"
use dsA, clear


tsset obsid cycle, delta(2) 

gen mainsamp_conviction = (ucshr >= .4 & ucshr <= .6 & odoubleprob == 0 & cycle != 2012)
gen mainsamp_indict = (ucshr >= .4 & ucshr <= .6 & odoubleprob == 0 & cycle != 2012)
gen mainsamp_olmsaudits = (ucshr >= .4 & ucshr <= .6 & odoubleprob == 0 & cycle >= 2006)

replace conviction = convcnt

gen anyconviction = (conviction != . & conviction > 0)
gen Lanyconviction = L.anyconviction
gen Lanyolmsaudits = L.anyolmsaudits

foreach y of varlist conviction indict olmsaudits { 

	qui: cgmreg Lany`y' c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp_`y' == 1 & cycle >= 2002, cluster(uid congdid)
	qui: estimates store e1
	qui: sum Lany`y' [aw=shrofcdpopincz] if e(sample) == 1
	qui: estadd scalar dvmean = `r(mean)'
	qui: unique rid if e(sample) == 1
	qui: estadd scalar N_rid = `r(unique)'

	qui: cgmreg any`y' c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp_`y' == 1, cluster(uid congdid)
	qui: estimates store e2
	qui: sum any`y' [aw=shrofcdpopincz] if e(sample) == 1
	qui: estadd scalar dvmean = `r(mean)'
	qui: unique rid if e(sample) == 1
	qui: estadd scalar N_rid = `r(unique)'

	qui: cgmreg `y' c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp_`y' == 1, cluster(uid congdid)
	qui: estimates store e3
	qui: sum `y' [aw=shrofcdpopincz] if e(sample) == 1
	qui: estadd scalar dvmean = `r(mean)'
	qui: unique rid if e(sample) == 1
	qui: estadd scalar N_rid = `r(unique)'

	qui: cgmreg Lany`y' c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp_`y' == 1 & cycle >= 2002 & ucshr >= .45 & ucshr <= .55, cluster(uid congdid)
	qui: estimates store e4
	qui: sum Lany`y' [aw=shrofcdpopincz] if e(sample) == 1
	qui: estadd scalar dvmean = `r(mean)'
	qui: unique rid if e(sample) == 1
	qui: estadd scalar N_rid = `r(unique)'

	qui: cgmreg any`y' c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp_`y' == 1 & ucshr >= .45 & ucshr <= .55, cluster(uid congdid)
	qui: estimates store e5
	qui: sum any`y' [aw=shrofcdpopincz] if e(sample) == 1
	qui: estadd scalar dvmean = `r(mean)'
	qui: unique rid if e(sample) == 1
	qui: estadd scalar N_rid = `r(unique)'

	qui: cgmreg `y' c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp_`y' == 1 & ucshr >= .45 & ucshr <= .55, cluster(uid congdid)
	qui: estimates store e6
	qui: sum `y' [aw=shrofcdpopincz] if e(sample) == 1
	qui: estadd scalar dvmean = `r(mean)'
	qui: unique rid if e(sample) == 1
	qui: estadd scalar N_rid = `r(unique)'

	estout e2 e3 e1 e5 e6 e4, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid N_rid, fmt(3 3 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
	cd "$loc_output"
	estout e2 e3 e1 e5 e6 e4 using T2_`y'.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid N_rid, fmt(3 3 0 0 0)) ///
		starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
		substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e1" "%&          e1" "N_uid" "N of union-CZ's" "N_rid" "N of Elections" ///
		"N_congdid" "N of Districts") label
	*
}


* Table 3 (Table B10):

cd "$loc_adata"
use dsA, clear

gen incumwin = incumR*gwinR + incumD*gwinD
preserve
  collapse (mean) incumwin, by(cycle congdid cz)
  gen ones = 1
  egen num = sum(incumwin), by(cycle cz)
  replace num = num - incumwin
  egen den = sum(ones), by(cycle cz)
  replace den = den - ones
  gen odincumwinshr = num/den
  sum odincumwinshr , d
  drop ones den num incumwin
  cd "$loc_temp"
  save ydc_iwin, replace
restore

cd "$loc_temp"
merge m:1 cycle congdid cz using ydc_iwin
drop _merge

keep if ucshr >= .4 & ucshr <= .6 & ccnt >= 3 & shrofcdpopincz >= .1 & cycle != 2012

gen ucowclose = (countR - closeR)/(ccnt - 1)
tab ucowclose ccnt if cycle != 2012 & ucshr >= .4 & ucshr <= .6, missing

gen pivotal = (ucowclose == 1 | ucowclose == 0)

* Won all others
cgmreg indict c.ucwin##c.(runvar) if cycle != 2012 & ucshr >= .4 & ucshr <= .6 & ccnt >= 3 & ucowclose == 1 & shrofcdpopincz >= .1, cluster(uid congdid)
estimates store e1
estadd scalar myN = e(N)
estadd scalar myr2 = e(r2)
estadd scalar myN_uid = e(N_uid)
estadd scalar myN_rid = e(N_congdid)
sum indict if e(sample) == 1
estadd scalar dvmean = `r(mean)'

* Lost all others
cgmreg indict c.ucwin##c.(runvar) if cycle != 2012 & ucshr >= .4 & ucshr <= .6 & ccnt >= 3 & ucowclose == 0 & shrofcdpopincz >= .1, cluster(uid congdid)
estimates store e2
estadd scalar myN = e(N)
estadd scalar myr2 = e(r2)
estadd scalar myN_uid = e(N_uid)
estadd scalar myN_rid = e(N_congdid)
sum indict if e(sample) == 1
estadd scalar dvmean = `r(mean)'

* All pivotal elections
cgmreg indict c.ucwin##c.(runvar) if cycle != 2012 & ucshr >= .4 & ucshr <= .6 & ccnt >= 3 & (ucowclose == 1 | ucowclose == 0) & shrofcdpopincz >= .1, cluster(uid congdid)
estimates store e3
estadd scalar myN = e(N)
estadd scalar myr2 = e(r2)
estadd scalar myN_uid = e(N_uid)
estadd scalar myN_rid = e(N_congdid)
sum indict if e(sample) == 1
estadd scalar dvmean = `r(mean)'

* Non-pivotal elections
cgmreg indict c.ucwin##c.(runvar) if cycle != 2012 & ucshr >= .4 & ucshr <= .6 & ccnt >= 3 & ucowclose > 0 & ucowclose < 1 & shrofcdpopincz >= .1, cluster(uid congdid)
estimates store e4
estadd scalar myN = e(N)
estadd scalar myr2 = e(r2)
estadd scalar myN_uid = e(N_uid)
estadd scalar myN_rid = e(N_congdid)
sum indict if e(sample) == 1
estadd scalar dvmean = `r(mean)'

estout e1 e2 e3 e4, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean myr2 myN myN_uid myN_rid, fmt(3 3 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin)

cgmreg indict c.ucwin##c.pivotal rhs lhs c.pivotal#c.(rhs lhs) if cycle != 2012 & ucshr >= .4 & ucshr <= .6 & ccnt >= 3 & shrofcdpopincz >= .1, cluster(uid congdid)


gen lnczpop = log(tpopcz)

tabstat Llogmembers lnczpop presvshrR cdpercz ucraceshr ccnt Lawingap lntsp lncam ucD ucisincum odincumwinshr, s(mean sd N) by(pivotal)
gen nonpiv = 1 - pivotal
foreach v of varlist Llogmembers lnczpop presvshrR cdpercz ucraceshr ccnt Lawingap lntsp lncam ucD ucisincum odincumwinshr {
  cgmreg `v' nonpiv, cluster(uid congdid)
}


replace Llogmembers = 0 if Lposmembers == 0

logit pivotal Lposmembers Llogmembers cdpercz ccnt
predict fitpiv, pr

tab pivotal, missing
gen pwt = .
replace pwt = 1/fitpiv if pivotal == 1
replace pwt = 1/(1-fitpiv) if pivotal == 0

* All pivotal elections
cgmreg indict c.ucwin##c.(runvar) if cycle != 2012 & ucshr >= .4 & ucshr <= .6 & ccnt >= 3 & (ucowclose == 1 | ucowclose == 0) & shrofcdpopincz >= .1 [aw=pwt], cluster(uid congdid)
estimates store e5
estadd scalar myN = e(N)
estadd scalar myr2 = e(r2)
estadd scalar myN_uid = e(N_uid)
estadd scalar myN_rid = e(N_congdid)
sum indict if e(sample) == 1  [aw=fitpiv]
estadd scalar dvmean = `r(mean)'

* Non-pivotal elections
cgmreg indict c.ucwin##c.(runvar) if cycle != 2012 & ucshr >= .4 & ucshr <= .6 & ccnt >= 3 & ucowclose > 0 & ucowclose < 1 & shrofcdpopincz >= .1 [aw=pwt], cluster(uid congdid)
estimates store e6
estadd scalar myN = e(N)
estadd scalar myr2 = e(r2)
estadd scalar myN_uid = e(N_uid)
estadd scalar myN_rid = e(N_congdid)
sum indict if e(sample) == 1  [aw=fitpiv]
estadd scalar dvmean = `r(mean)'

estout e1 e2 e3 e4 e5 e6, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean myr2 myN myN_uid myN_rid, fmt(3 3 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
cd "$loc_output"
estout e1 e2 e3 e4 e5 e6 using T3.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid, fmt(3 3 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e1" "%&          e1" "N_uid" "N of union-CZ's" "N_rid" "N of elections" ///
	"N_congdid" "N of Districts") label

**********************
/*	SENATE SHARE	*/
**********************

cd "$loc_rawh"
insheet using Sall_members.csv, comma clear

keep if chamber == "Senate"
list in 1/1
gen rshr = (party_code == 200)

gen officeyear = (congress - 100)*2 + 1987
tab congress officeyear if officeyear >= 2000
* Accurate: The 107th Congress was in office 2001-2002 (office year == 2001) 
* https://en.wikipedia.org/wiki/107th_United_States_Congress
gen cycle = officeyear - 1

keep if cycle >= 2000 & cycle <= 2014

collapse (mean) rshr [aw=number_of_votes], by(state_abbrev cycle)

replace rshr = round(rshr,.5)
tab rshr

rename state_abbrev stabb
cd "$loc_rawh"
merge m:1 stabb using cw_state
keep if _merge == 3
keep stfips rshr cycle
rename stfips fipstate

cd "$loc_adata"
merge 1:m fipstate cycle using dsA
drop _merge

gen spshr = ucD*(1-rshr) + (1-ucD)*rshr

keep if ucshr >= .4 & ucshr <= .6 & ccnt >= 3 & shrofcdpopincz >= .1 & cycle != 2012

gen ucowclose = (countR - closeR)/(ccnt - 1)
tab ucowclose ccnt if cycle != 2012 & ucshr >= .4 & ucshr <= .6, missing

gen pivotal = (ucowclose == 1 | ucowclose == 0)

tabstat spshr, s(mean sd N) by(pivotal)

gen nonpiv = 1 - pivotal
foreach v of varlist spshr {
  cgmreg `v' nonpiv, cluster(uid congdid)
}


* Table B2:

cd "$loc_adata"
use dsA if cycle < 2012 & shrofcdpopincz >= .1 & runvar != ., clear


keep if odoubleprob == 0

cgmreg anyindict c.ucwin##c.(runvar) if ucshr >= .4 & ucshr <= .6, cluster(uid congdid)
reg anyindict c.ucwin##c.(runvar) if ucshr >= .4 & ucshr <= .6, cluster(uid)
reg anyindict c.ucwin##c.(runvar) if ucshr >= .4 & ucshr <= .6, cluster(congdid)

sum anyindict if ucshr >= .4 & ucshr <= .6

rdrobust anyindict runvar, vce(cluster uid)
rdrobust anyindict runvar, vce(cluster congdid)

drop cord
sort uid cycle arunvar
by uid cycle: gen cord = _n

keep if cord == 1

reg anyindict c.ucwin##c.(runvar) if ucshr >= .4 & ucshr <= .6, cluster(congdid)
rdrobust anyindict runvar, vce(cluster congdid)



*
* Table B3: 

cd "$loc_adata"
use dsA, clear

* 40-60
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .4 & ucshr <= .6 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e1
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .4 & ucshr <= .6, cluster(uid congdid)
estimates store e2
sum anyindict if e(sample) == 1
estadd scalar dvmean = `r(mean)'
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .4 & ucshr <= .6 & shrofcdpopincz >= .1, cluster(uid congdid)
estimates store e3
sum anyindict if e(sample) == 1
estadd scalar dvmean = `r(mean)'
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .4 & ucshr <= .6 & shrofcdpopincz < .1, cluster(uid congdid)
estimates store e4
sum anyindict if e(sample) == 1
estadd scalar dvmean = `r(mean)'

estout e1 e2 e3 e4, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N r2 dvmean, fmt(0 3 3)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
cd "$loc_output"
estout e1 e2 e3 e4 using TB3a.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid, fmt(3 3 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e1" "%&          e1" "N_uid" "N of union-CZ's" "N_rid" "N of elections" ///
	"N_congdid" "N of Districts") label
*

* 45-55
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e1
sum anyindict if e(sample) == 1
estadd scalar dvmean = `r(mean)'
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55, cluster(uid congdid)
estimates store e2
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 & shrofcdpopincz >= .1, cluster(uid congdid)
estimates store e3
sum anyindict if e(sample) == 1
estadd scalar dvmean = `r(mean)'
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 & shrofcdpopincz < .1, cluster(uid congdid)
estimates store e4
sum anyindict if e(sample) == 1
estadd scalar dvmean = `r(mean)'

estout e1 e2 e3 e4, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N r2 dvmean, fmt(0 3 3)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
cd "$loc_output"
estout e1 e2 e3 e4 using TB3b.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid, fmt(3 3 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e1" "%&          e1" "N_uid" "N of union-CZ's" "N_rid" "N of elections" ///
	"N_congdid" "N of Districts") label
*

* Table B4:

cd "$loc_adata"
use dsA, clear

encode unm, gen(nunm)

* 40-60
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .4 & ucshr <= .6 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e2
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .4 & ucshr <= .6 [aw=shrofcdpopincz], cluster(cz congdid)
estimates store e3
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .4 & ucshr <= .6 [aw=shrofcdpopincz], cluster(cz fipstate)
estimates store e4
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .4 & ucshr <= .6 [aw=shrofcdpopincz], cluster(nunm cz fipstate)
estimates store e5

estout e2 e3 e4 e5, cells(b(star fmt(%9.4f)) se(par fmt(%9.4f)) p(par([ ]) fmt(%9.3f))) stats(N r2, fmt(0 3 3)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
cd "$loc_output"
estout e2 e3 e4 e5 using TB4a.tex, cells(b(star fmt(%9.4f)) se(par fmt(%9.4f)) p(par([ ]) fmt(%9.3f))) stats(r2 N, fmt(3 3 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e2" "%&          e1" "N_uid" "N of union-CZ's" "N_rid" "N of elections" ///
	"N_congdid" "N of Districts") label
*

* 45-55
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e2
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 [aw=shrofcdpopincz], cluster(cz congdid)
estimates store e3
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 [aw=shrofcdpopincz], cluster(cz fipstate)
estimates store e4
cgmreg anyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 [aw=shrofcdpopincz], cluster(nunm cz fipstate)
estimates store e5

estout e2 e3 e4 e5, cells(b(star fmt(%9.4f)) se(par fmt(%9.4f)) p(par([ ]) fmt(%9.3f))) stats(N r2, fmt(0 3 3)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
cd "$loc_output"
estout e2 e3 e4 e5 using TB4b.tex, cells(b(star fmt(%9.4f)) se(par fmt(%9.4f)) p(par([ ]) fmt(%9.3f))) stats(r2 N, fmt(3 3 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e2" "%&          e1" "N_uid" "N of union-CZ's" "N_rid" "N of elections" ///
	"N_congdid" "N of Districts") label
*

* Table B5:

cd "$loc_adata"
use dsA, clear

tabmiss ucwin
egen mucwin = mean(ucwin), by(unm cz)
egen mucwinc = mean(ucwin), by(unm cz cycle)

gen poscamR = (camR > 0)
tab poscamR if camD > 0		// 1.7% of the time when you give to a Dem, you also give to a Republican
tabmiss ucshr if poscamR == 1 & camD > 0		// I didn't define ucshr for these folks
gen wtf = camD/(camR+camD)
sum wtf if poscamR == 1 & camD > 0, d			// On average (and median), if you give to both, 2/3 goes to Dem

gen ucshrB = ucshr
replace ucshrB = gvshrD if wtf > .5 & wtf != . & ucshr == .
replace ucshrB = gvshrR if wtf < .5 & wtf != . & ucshr == .
gen runvarB = ucshrB - .5
gen runvar2B = runvarB^2
gen ucwinB = (ucshrB > .5)
replace ucwinB = . if ucshrB == .

egen everuc = sum(ucD), by(fipstate congdist cycle)

***	ASSORTED EXTRA ROBUSTNESS

gen temp_ucwin = ucwin
replace ucwin = ucwinB

cgmreg anyindict c.ucwin##c.(runvarB) if ucshrB >= .4 & ucshrB <= .6 & odoubleprob == 0 & cycle != 2012 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store bonq1
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg anyindict c.ucwin##c.(runvarB) if ucshrB >= .45 & ucshrB <= .55 & odoubleprob == 0 & cycle != 2012 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store bonl1
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

replace ucwin = temp_ucwin
drop temp_ucwin

cgmreg indict c.ucwin##c.(runvar) if ucshr >= .4 & ucshr <= .6 & odoubleprob == 0 & cycle != 2012 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store bonq2
sum indict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg indict c.ucwin##c.(runvar) if ucshr >= .45 & ucshr <= .55 & odoubleprob == 0 & cycle != 2012 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store bonl2
sum indict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

* Closest race only

preserve
	sort unm cz cycle arunvar
	by unm cz cycle: gen ord = _n
	sum arunvar if ord == 1, d
	sum arunvar if ord != 1, d
	keep if ord == 1
	cgmreg anyindict c.ucwin##c.(runvar) if ucshr >= .4 & ucshr <= .6 & odoubleprob == 0 & cycle != 2012 [aw=shrofcdpopincz], cluster(uid congdid)
	estimates store bonq3
	sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
	estadd scalar dvmean = `r(mean)'	
	cgmreg anyindict c.ucwin##c.(runvar) if ucshr >= .45 & ucshr <= .55 & odoubleprob == 0 & cycle != 2012 [aw=shrofcdpopincz], cluster(uid congdid)
	estimates store bonl3
	sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
	estadd scalar dvmean = `r(mean)'
restore

* Dropping mob-cities

gen mob = ""
replace mob = "san francisco" if cz == 37800
replace mob = "san jose" if cz == 37500
replace mob = "la" if cz == 38300
replace mob = "denver" if cz == 28900
replace mob = "tampa" if cz == 6700
replace mob = "chicago" if cz == 24300
replace mob = "new orleans" if cz == 3300
replace mob = "boston" if cz == 20500
replace mob = "detroit" if cz == 11600
replace mob = "kansas city" if cz == 29502
replace mob = "st louis!!! woo!!!" if cz == 24701
replace mob = "newark" if cz == 19600
replace mob = "new york" if cz == 19400
replace mob = "buffalo/rochester" if cz == 18000
replace mob = "nyc" if cz == 19400
replace mob = "cleveland" if cz == 15200
replace mob = "philadelphia" if cz == 19700
replace mob = "pittsburgh" if cz == 16300
replace mob = "dallas" if cz == 33100
replace mob = "milwaukee" if cz == 21501
gen nmob = (mob != "")

cgmreg anyindict c.ucwin##c.(runvar) if ucshr >= .4 & ucshr <= .6 & odoubleprob == 0 & cycle != 2012 & nmob == 0 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store bonq4
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg anyindict c.ucwin##c.(runvar) if ucshr >= .45 & ucshr <= .55 & odoubleprob == 0 & cycle != 2012 & nmob == 0 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store bonl4
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg anyindict c.ucwin##c.(runvar) if ucshr >= .4 & ucshr <= .6 & odoubleprob == 0 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store bonq5
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg anyindict c.ucwin##c.(runvar) if ucshr >= .45 & ucshr <= .55 & odoubleprob == 0 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store bonl5
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmlogit anyindict ucwin lhs rhs if mainsamp == 1 [iw=shrofcdpopincz], cluster(uid congdid)
estimates store bonq6
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmlogit anyindict ucwin lhs rhs if ucshr >= .45 & ucshr <= .55 & mainsamp == 1 [iw=shrofcdpopincz], cluster(uid congdid)
estimates store bonl6
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

estout bonq1 bonq2 bonq3 bonq4 bonq5 bonq6, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N r2 dvmean, fmt(0 3 3)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
cd "$loc_output"
estout bonq1 bonq2 bonq3 bonq4 bonq5 bonq6 using TB5a.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid, fmt(3 3 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e1" "%&          e1" "N_uid" "N of union-CZ's" "N_rid" "N of elections" ///
	"N_congdid" "N of Districts") label
*
estout bonl1 bonl2 bonl3 bonl4 bonl5 bonl6, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N r2 dvmean, fmt(0 3 3)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
cd "$loc_output"
estout bonl1 bonl2 bonl3 bonl4 bonl5 bonl6 using TB5b.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid, fmt(3 3 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e1" "%&          e1" "N_uid" "N of union-CZ's" "N_rid" "N of elections" ///
	"N_congdid" "N of Districts") label
*

* Table B6:

cd "$loc_adata"
use dsA, clear


***	SMOOTHNESS	***

cgmreg Lposmembers c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp == 1 & cycle != 2000, cluster(uid congdid)
estimates store e1
sum Lposmembers [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg Llogmembers c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp == 1 & cycle != 2000, cluster(uid congdid)
estimates store e2
sum Llogmembers [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg Llogttl_receipts c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp == 1 & cycle != 2000, cluster(uid congdid)
estimates store e3
sum Llogttl_receipts [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg Lanyindict c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp == 1 & cycle != 2000, cluster(uid congdid)
estimates store e4
sum Lanyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg lncam c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp == 1, cluster(uid congdid)
estimates store e5
sum lncam [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg ucraceshr c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp == 1, cluster(uid congdid)
estimates store e6
sum ucraceshr [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg ucisincum c.ucwin##c.(runvar) [aw=shrofcdpopincz] if mainsamp == 1, cluster(uid congdid)
estimates store e7
sum ucisincum [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

estout e1 e2 e3 e4 e5 e6 e7, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N r2 dvmean depvar, fmt(0 3 3)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
cd "$loc_output"
estout e1 e2 e3 e4 e5 e6 e7 using TB6a.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid, fmt(3 3 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e1" "%&          e1" "N_uid" "N of union-CZ's" "N_rid" "N of elections" ///
	"N_congdid" "N of Districts") label
*

cgmreg Lposmembers c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 & cycle != 2000 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e1
sum Lposmembers [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg Llogmembers c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 & cycle != 2000 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e2
sum Llogmembers [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg Llogttl_receipts c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 & cycle != 2000 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e3
sum Llogttl_receipts [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg Lanyindict c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 & cycle != 2000 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e4
sum Lanyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg lncam c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e5
sum lncam [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg ucraceshr c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e6
sum ucraceshr [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg ucisincum c.ucwin##c.(runvar) if mainsamp == 1 & ucshr >= .45 & ucshr <= .55 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e7
sum ucisincum [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

estout e1 e2 e3 e4 e5 e6 e7, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N r2 dvmean depvar, fmt(0 3 3)) starlevels(* .10 ** .05 *** .01) keep(ucwin)
cd "$loc_output"
estout e1 e2 e3 e4 e5 e6 e7 using TB6b.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid, fmt(3 3 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e1" "%&          e1" "N_uid" "N of union-CZ's" "N_rid" "N of elections" ///
	"N_congdid" "N of Districts") label
*

* Table B7:

cd "$loc_adata"
use dsA, clear

*gen ucD = (ucwin == gwinD)
gen runvarD = gvshrD - .5
gen runvarD2 = runvarD^2

replace ucD = . if ucrace == 0
egen temp = mean(ucD), by(rid)
sum temp, d
sum ucshr if temp > 0 & temp < 1, d
gen nonagree = (temp > 0 & temp < 1)
drop temp

sum ucshr if ucD == 0 [aw=shrofcdpopincz], d
sum ucshr if ucD == 0 & ucshr >= .3 & ucshr <= .7 [aw=shrofcdpopincz], d
tab ucwin if ucD == 0 & ucshr >= .3 & ucshr <= .7 [aw=shrofcdpopincz]
tab ucwin if ucD == 0 & ucshr >= .3 & ucshr <= .7

tabmiss ucwin
egen mucwin = mean(ucwin), by(unm cz)
egen mucwinc = mean(ucwin), by(unm cz cycle)

gen poscamR = (camR > 0)
tab poscamR if camD > 0		// 1.7% of the time when you give to a Dem, you also give to a Republican
tabmiss ucshr if poscamR == 1 & camD > 0		// I didn't define ucshr for these folks
gen wtf = camD/(camR+camD)
sum wtf if poscamR == 1 & camD > 0, d			// On average (and median), if you give to both, 2/3 goes to Dem

gen ucshrB = ucshr
replace ucshrB = gvshrD if wtf > .5 & wtf != . & ucshr == .
replace ucshrB = gvshrR if wtf < .5 & wtf != . & ucshr == .
gen runvarB = ucshrB - .5
gen runvar2B = runvarB^2
gen ucwinB = (ucshrB > .5)
replace ucwinB = . if ucshrB == .

egen everuc = sum(ucD), by(fipstate congdist cycle)

cgmreg anyindict c.ucwin##c.(runvar) if ucshr >= .4 & ucshr <= .6 & odoubleprob == 0 & cycle != 2012 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e1
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg anyindict c.gwinD##c.(runvarD) if gvshrD >= .4 & gvshrD <= .6 & ucrace == 0 & mucwin != . & odoubleprob == 0 & cycle != 2012 & everuc != 0 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e2
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg anyindict c.gwinD##c.(runvarD) if gvshrD >= .4 & gvshrD <= .6 & ucrace == 0 & mucwinc != . & odoubleprob == 0 & cycle != 2012 & everuc != 0 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e3
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

reghdfe anyindict c.ucwin##c.(runvar) if ucshr >= .4 & ucshr <= .6 & nonagree == 1 & odoubleprob == 0 & cycle != 2012 [aw=shrofcdpopincz], absorb(rid) cluster(uid congdid)
estimates store e4
unique congdid if e(sample) == 1
unique congdid
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'
estadd scalar N_uid = e(N_clust1)
estadd scalar N_rid = e(N_clust2)

estout e1 e2 e3 e4, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_rid, fmt(3 3 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin gwinD)
cd "$loc_output"
estout e1 e2 e3 e4 using TB7a.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid, fmt(3 3 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin gwinD) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e1" "%&          e1" "N_uid" "N of union-CZ's" "N_rid" "N of elections" ///
	"N_congdid" "N of Districts") label
*

preserve
  gen sampB = (gvshrD >= .4 & gvshrD <= .6 & ucrace == 0 & mucwin != . & odoubleprob == 0 & cycle != 2012)
  cd "$loc_temp"
  save temp, replace
  replace sampB = 0
  gen sampA = (ucshr >= .4 & ucshr <= .6 & odoubleprob == 0 & cycle != 2012)
  gen win = ucwin
  gen rv = runvar
  append using temp
  replace sampA = 0 if sampA == .
  replace win = gwinD if win == .
  replace rv = runvarD if rv == .
  tab sampA sampB
  cgmreg anyindict c.win##c.rv##c.sampA if sampA == 1 | sampB == 1 [aw=shrofcdpopincz], cluster(uid congdid) 
  replace sampB = (gvshrD >= .45 & gvshrD <= .55 & ucrace == 0 & mucwin != . & odoubleprob == 0 & cycle != 2012) if sampB == 1
  replace sampA = (ucshr >= .45 & ucshr <= .55 & odoubleprob == 0 & cycle != 2012) if sampA == 1
  tab sampA sampB
  cgmreg anyindict c.win##c.rv##c.sampA if sampA == 1 | sampB == 1 [aw=shrofcdpopincz], cluster(uid congdid) 
restore


cgmreg anyindict c.ucwin##c.(runvar) if ucshr >= .45 & ucshr <= .55 & odoubleprob == 0 & cycle != 2012 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e1
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg anyindict c.gwinD##c.(runvarD) if gvshrD >= .45 & gvshrD <= .55 & ucrace == 0 & mucwin != . & odoubleprob == 0 & cycle != 2012 & everuc != 0 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e2
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

cgmreg anyindict c.gwinD##c.(runvarD) if gvshrD >= .45 & gvshrD <= .55 & ucrace == 0 & mucwinc != . & odoubleprob == 0 & cycle != 2012 & everuc != 0 [aw=shrofcdpopincz], cluster(uid congdid)
estimates store e3
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'

reghdfe anyindict c.ucwin##c.(runvar) if ucshr >= .45 & ucshr <= .55 & nonagree == 1 & odoubleprob == 0 & cycle != 2012 [aw=shrofcdpopincz], absorb(rid) cluster(uid congdid)
estimates store e4
unique congdid if e(sample) == 1
unique congdid
sum anyindict [aw=shrofcdpopincz] if e(sample) == 1
estadd scalar dvmean = `r(mean)'
estadd scalar N_uid = e(N_clust1)
estadd scalar N_rid = e(N_clust2)

estout e1 e2 e3 e4, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_rid, fmt(3 3 0 0 0)) starlevels(* .10 ** .05 *** .01) keep(ucwin gwinD)
cd "$loc_output"
estout e1 e2 e3 e4 using TB7b.tex, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(dvmean r2 N N_uid N_congdid, fmt(3 3 0 0 0)) ///
	starlevels(* .10 ** .05 *** .01) keep(ucwin gwinD) style(tex) replace ///
	substitute("r2 " " $ R^2 $ " "dvmean" "DV Mean" "&        b/se" "%&        %b/se" "&          e1" "%&          e1" "N_uid" "N of union-CZ's" "N_rid" "N of elections" ///
	"N_congdid" "N of Districts") label
*






*










*